home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / utils / win32 / mv.c < prev    next >
C/C++ Source or Header  |  1995-08-27  |  3KB  |  177 lines

  1.  
  2.  
  3. /*
  4.  *
  5.  *    Copyright 1993 Algorithms Corporation
  6.  *
  7.  *    ALL RIGHTS RESERVED.
  8.  *
  9.  *
  10.  *
  11.  */
  12.  
  13.  
  14.  
  15.  
  16.  
  17. #include <stdio.h> 
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <dos.h>
  21. #include <windows.h>
  22.  
  23. static    void    strip_path();
  24. static    void    usage();
  25. static    int    rm();
  26.  
  27.  
  28. int    main(argc, argv)
  29. int    argc;
  30. char    *argv[];
  31. {
  32.     int    r, i, isdir, err=0, quiet=0, force=0, existing=0, verbose=0, zero=0;
  33.     struct    stat    sb;
  34.     char    buf[128], tmp[128];
  35.     
  36.     if (argc < 3)
  37.         usage();
  38.     strip_path(tmp, argv[argc-1]);
  39.     if (!*tmp  ||  !strcmp(tmp, ".")  ||  !strcmp(tmp, ".."))
  40.         isdir = 1;
  41.     else  {
  42.         r = stat(argv[argc-1], &sb);
  43.         isdir = !r  &&  (sb.st_mode & S_IFDIR);
  44.     }
  45.     argc--;
  46.     for ( ; 1 < argc  &&  argv[1][0] == '-' ; argc--, argv++)
  47.         for (r=1 ; argv[1][r] ; ++r)
  48.             switch (argv[1][r])  {
  49.             case 'e':
  50.             case 'E':
  51.                 existing = 1;
  52.                 break;
  53.             case 'f':
  54.             case 'F':
  55.                 force = 1;
  56.                 break;
  57.             case 'q':
  58.             case 'Q':
  59.                 quiet = 1;
  60.                 verbose = 0;
  61.                 break;
  62.             case 'v':
  63.             case 'V':
  64.                 verbose = 1;
  65.                 quiet = 0;
  66.                 break;
  67.             case 'z':
  68.             case 'Z':
  69.                 zero = 1;
  70.                 break;
  71.             default:
  72.                 fprintf(stderr, "mv:  Unknown flag %c\n", argv[i][r]);
  73.                 usage();
  74.                 break;
  75.             }
  76.     if (argc > 2  &&  !isdir  ||  argc < 2)
  77.         usage();
  78.     for (i=1 ; i < argc ; ++i)  {
  79.         r = stat(argv[i], &sb);
  80.         if (r)  {
  81.             if (!quiet)
  82.                 fprintf(stderr, "mv:  source file %s doesn't exist\n", argv[i]);
  83.             err = 1;
  84.             continue;
  85.         }
  86.         if ((sb.st_mode & S_IFDIR)  &&  isdir) {
  87.             if (!quiet)
  88.                 fprintf(stderr, "mv:  %s and %s are both directories\n", argv[i], argv[argc]);
  89.             err = 1;
  90.             continue;
  91.         }
  92.         if (isdir)  {
  93.             char    c;
  94.  
  95.             strip_path(tmp, argv[i]);
  96.             strcpy(buf, argv[argc]);
  97.             c = buf[strlen(buf)-1];
  98.             if (c != '/'  &&  c != '\\'  &&  c != ':')
  99.                 strcat(buf, "/");
  100.             strcat(buf, tmp);
  101.             if (!existing)
  102.                 rm(buf, force);
  103.             r = rename(argv[i], buf);
  104.             if (r)  {
  105.                 if (!quiet)
  106.                     fprintf(stderr, "mv:  Can't move %s to %s\n", argv[i], buf);
  107.                 err = 1;
  108.             } else if (verbose)
  109.                 fprintf(stderr, "Moving %s -> %s\n", argv[i], buf);
  110.         }  else  {
  111.             if (!existing)
  112.                 rm(argv[argc], force);
  113.             r = rename(argv[i], argv[argc]);
  114.             if (r)  {
  115.                 if (!quiet)
  116.                     fprintf(stderr, "mv:  Can't move %s to %s\n", argv[i], argv[argc]);
  117.                 err = 1;
  118.             } else if (verbose)
  119.                 fprintf(stderr, "Moving %s -> %s\n", argv[i], argv[argc]);
  120.         }
  121.     }
  122.     return zero ? 0 : err;
  123. }
  124.  
  125. static    void    usage()
  126. {
  127.     printf("Usage:\tmv  [-options]  file  file\n"); 
  128.     printf("\tmv  [-options]  file...  directory\n");
  129.     printf("Options:\n");
  130.     printf("\te\tdon't distroy pre-existing files\n");
  131.     printf("\tf\tforce (even if read only)\n");
  132.     printf("\tq\tquiet (no error messages)\n");
  133.     printf("\tv\tverbose\n");
  134.     printf("\tz\talways return 0 exist status\n");
  135.     exit(1); 
  136. }
  137.  
  138. static    void    strip_path(to, from)
  139. char    *from, *to;
  140. {
  141.     char    *t;
  142.  
  143.     for (t=from ; *t ; ++t)
  144.         if (*t == '/'  ||  *t == '\\'  ||  *t == ':')
  145.             from = t + 1;
  146.     strcpy(to, from);
  147. }
  148.  
  149. static    int    rm(f, force)
  150. char    *f;
  151. int    force;
  152. {
  153.     int    r;
  154.  
  155.     r = unlink(f);
  156.     if (r  &&  force)  {
  157.         SetFileAttributes(f, FILE_ATTRIBUTE_NORMAL);
  158.         r = unlink(f);
  159.     }
  160.     return(r);
  161. }
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.  *
  168.  *    Copyright 1993 Algorithms Corporation
  169.  *
  170.  *    ALL RIGHTS RESERVED.
  171.  *
  172.  *
  173.  *
  174.  */
  175.  
  176.  
  177.